package org.tankwar.model;

import org.tankwar.type.Direction;
import org.tankwar.type.PlayerType;
import org.tankwar.util.GlobalUtil;
import org.tankwar.util.ImageUtil;
import org.tankwar.view.GamePanel;
import java.awt.*;

public class PlayerTank extends Tank{
    public PlayerTank(int x, int y, Direction direction, GamePanel frame, PlayerType player) {
        switch(player) {
            case PLAYER_ONE:
                this.tankImages = new Image[] {
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER1_DOWN_IMAGE_URL)),
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER1_UP_IMAGE_URL)),
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER1_LEFT_IMAGE_URL)),
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER1_RIGHT_IMAGE_URL)), };
                break;
            case PLAYER_TWO:
                this.tankImages = new Image[] {
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER2_DOWN_IMAGE_URL)),
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER2_UP_IMAGE_URL)),
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER2_LEFT_IMAGE_URL)),
                        GlobalUtil.tk.getImage(this.getClass().getResource(ImageUtil.PLAYER2_RIGHT_IMAGE_URL)), };
                break;
            default:
                break;
        }
        this.good = true;
        this.width = 35;
        this.length = 35;
        this.x = x;
        this.y = y;
        this.oldX = x;
        this.oldY = y;
        this.status = direction;
        this.tc = frame;
        this.life = 200;
    }

    @Override
    public void draw(Graphics g) {
        if (!live) return;
        new DrawBloodBar().draw(g);
        drawTank(g);
        move();
    }

    private class DrawBloodBar {
        public void draw(Graphics g) {
            Color c = g.getColor();
            g.setColor(Color.RED);
            g.drawRect(375, 585, width, 10);// 显示玩家坦克的血量条
            int w = width * life / 200;
            g.fillRect(375, 585, w, 10);// 显示玩家坦克的血量条
            g.setColor(c);
        }
    }

    @Override
    public void move() {
        decideDirection(); // 先判断坦克要往哪里移动
        baseMove();
    }

    private void decideDirection() {
        if (!toL && !toU && toR && !toD) // 向右移动
            status = Direction.R;
        else if (toL && !toU && !toR && !toD) // 向左移
            status = Direction.L;
        else if (!toL && toU && !toR && !toD) // 向上移动
            status = Direction.U;
        else if (!toL && !toU && !toR && toD) // 向下移动
            status = Direction.D;
        else
            status = Direction.STOP; // 没有按键，就保持不动
    }

    public boolean eat(Blood b) {
        if (this.live && b.isLive() && this.getRect().intersects(b.getRect())) {
            if (this.life <= 100)
                this.life = this.life + 100; // 每吃一个，增加100生命点
            else
                this.life = 200;
            b.setLive(false);
            return true;
        }
        return false;
    }
}
